/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.trandio.manager;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.trandio.dao.ActividadpypDAO;
import org.trandio.dao.ActividadpypInterface;
import org.trandio.dao.ContratoDAO;
import org.trandio.dao.ContratoInterface;
import org.trandio.dao.PypInterface;
import org.trandio.dao.pypDAO;
import org.trandio.model.ActividadpypVO;
import org.trandio.model.ConfigActividadDePypVO;
import org.trandio.model.ContratoVO;
import org.trandio.model.PlaneacionpypVO;

/**
 *
 * @author root
 */
public class PypServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    PypInterface pypInterface;
    ContratoInterface contratoInterface;
    ActividadpypInterface actividadpypInterface;
    ContratoVO contratoVO;
    ActividadpypVO actividadpypVO;
    PlaneacionpypVO planeacionpypVO;
    ConfigActividadDePypVO configActividadDePypVO;
    
    private List<PlaneacionpypVO> listPlanecionpyp;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        pypInterface = new pypDAO();
        contratoInterface = new ContratoDAO();
        actividadpypInterface = new ActividadpypDAO();


        try {


            int opcion = Integer.parseInt(request.getParameter("op"));
            
            
            switch (opcion) {
                
                case 1:
                    int paciente = Integer.parseInt(request.getParameter("id_paciente"));
                    int entidad  = Integer.parseInt(request.getParameter("id_entidad"));

                    try {
                        contratoVO = contratoInterface.getContratoPypEntidad(entidad);
                        pypInterface.programarPaciente(paciente, contratoVO.getId());
                    } catch (SQLException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    break;


                case 2:
                    int pacientepyp = Integer.parseInt(request.getParameter("id_paciente"));
                    try {
                        listPlanecionpyp = pypInterface.consultarPlaneacionPacientepyp(pacientepyp);
                    } catch (SQLException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }


                    Iterator tplanpyp = listPlanecionpyp.iterator();

                    out.println("<table id='tblplanpyp' width='100%' style='border:1px solid gray' class='extdt-caption'>");
                    //out.println("<tr class='ui-widget-header'><td colspan='6'>Turnos</td></tr>");
                    out.println("<tr class='ui-widget-header'><td>Programa</td><td>C&oacute;digo</td><td>Actividad</td><td>Cantidad Prog</td><td>Contrato</td><td>Eliminar</td></tr>");
                    while (tplanpyp.hasNext()) {
                        PlaneacionpypVO b = (PlaneacionpypVO) tplanpyp.next();

                        out.println("<tr><td>" + b.getPrograma() + "</td><td>" + b.getCodigoAct() + "</td><td align='center'>" + b.getActividad() + "</td><td align='center'>" + b.getCantidad() + "</td><td align='center'>" + b.getNumcontrato() + "</td>");
                        out.println("<td><img style='cursor:pointer' src='../common/img/del1.png' onclick='quitarPlaneacionPyp("+b.getId()+")' /></td></tr>");
                    }
                    out.println("</table>");


                    break;


                case 3:
                    int pac = Integer.parseInt(request.getParameter("paciente"));
                    int act = Integer.parseInt(request.getParameter("actividad"));

                    String resp = "";
                    try {
                        planeacionpypVO = pypInterface.consultarPlaneacionPacientepyp(pac, act);

                        int idprog = pypInterface.getEjecucionpyp(planeacionpypVO.getId());

                        if (idprog == 0) {
                            planeacionpypVO.setId(0);
                        }

                    } catch (SQLException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    resp = "{\"id\":\"" + planeacionpypVO.getId()
                            + "\",\"contrato\":\"" + planeacionpypVO.getId_contrato()
                            + "\",\"descontrato\":\"" + planeacionpypVO.getDescontrato()
                            + "\",\"programa\":\"" + planeacionpypVO.getPrograma() + "\"}";

                    out.println(resp);

                    break;

                case 4:
                    int idPlaneacion = Integer.parseInt(request.getParameter("id"));
                    try {
                        pypInterface.quitarPlaneacionPyp(idPlaneacion);
                    } catch (SQLException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    break;
                    
                    
                    
                case 5:
                    int idPac = Integer.parseInt(request.getParameter("paciente"));

                    String rta = "";
                    try {
                        //configActividadDePypVO = pypInterface.consultarConfigurcionActividadDePyp(int idActividad, int idSexo, int edad, int cantidad);
                        configActividadDePypVO = pypInterface.consultarConfigurcionActividadDePyp(1, 1, 1, 1);

                        int idprog = pypInterface.getEjecucionpyp(planeacionpypVO.getId());

                        if (idprog == 0) {
                            planeacionpypVO.setId(0);
                        }

                    } catch (SQLException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NamingException ex) {
                        Logger.getLogger(PypServlet.class.getName()).log(Level.SEVERE, null, ex);
                    }

                    resp = "{\"id\":\"" + planeacionpypVO.getId()
                            + "\",\"contrato\":\"" + planeacionpypVO.getId_contrato()
                            + "\",\"descontrato\":\"" + planeacionpypVO.getDescontrato()
                            + "\",\"programa\":\"" + planeacionpypVO.getPrograma() + "\"}";

                    out.println(resp);

                    break;

            }

        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
